﻿// CModifyClientDlg.cpp: 实现文件
//

#include "pch.h"
#include "HotelManagementMFC.h"
#include "afxdialogex.h"
#include "CModifyClientDlg.h"


// CModifyClientDlg 对话框

IMPLEMENT_DYNAMIC(CModifyClientDlg, CDialog)

CModifyClientDlg::CModifyClientDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DLG_CLIENT_MODIFY, pParent)
{

}

CModifyClientDlg::~CModifyClientDlg()
{
}

void CModifyClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//  DDX_Control(pDX, IDC_PAPERNUM, m_sPaperNum);
	DDX_Control(pDX, IDC_NUM, m_nNum);
	DDX_Control(pDX, IDC_NAME, m_nName);
	DDX_Control(pDX, IDC_SEX, m_nSex);
	DDX_Control(pDX, IDC_FROM, m_nFrom);
	DDX_Control(pDX, IDC_SEARCH_PAPERNUM, m_sPaperNum);
	DDX_Control(pDX, IDC_PAPERNUM, m_nPaperNum);
	DDX_Control(pDX, IDC_ROOMNUM, m_nRoomNum);
	DDX_Control(pDX, IDC_TIME, m_nTime);
	DDX_Control(pDX, IDC_PREPAYMENT, m_nPrepayment);
	DDX_Control(pDX, IDOK, m_bSave);
}


BEGIN_MESSAGE_MAP(CModifyClientDlg, CDialog)
	ON_BN_CLICKED(ID_SEARCH, &CModifyClientDlg::OnSearchClient)
	ON_BN_CLICKED(IDOK, &CModifyClientDlg::OnSaveClient)
END_MESSAGE_MAP()


// CModifyClientDlg 消息处理程序


void CModifyClientDlg::OnSearchClient()
{
	// TODO: 在此添加控件通知处理程序代码
	//UpdateData(1);
	CString search_Num;
	m_sPaperNum.GetWindowTextW(search_Num);
	if (search_Num == "")
	{
		MessageBoxW(L"证件号码不能为空，请重新输入！");
		return;
	}

	// SQL查询过程
	CString strSQL;
	strSQL.Format(L"Select ClientNo, ClientName, Sex, NativePlace, PaperNum, InDate, RoomNo, Deposit From Client Where PaperNum = '%s'", search_Num);

	try {
		m_pRecordSet.CreateInstance(__uuidof(Recordset));
		m_pRecordSet->Open(strSQL.AllocSysString(),
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);

		// 使保存按钮可用
		m_bSave.EnableWindow(1);
		// 遍历结果集
		CString tmp;
		_variant_t Holder;
		// 判断为空
		if (!m_pRecordSet->GetRecordCount() == 0)
		{
			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "ClientNo");
			m_nNum.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "ClientName");
			m_nName.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "Sex");
			m_nSex.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "NativePlace");
			m_nFrom.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "PaperNum");
			m_nPaperNum.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "InDate");
			m_nTime.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "RoomNo");
			m_nRoomNum.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "Deposit");
			m_nPrepayment.SetWindowTextW(tmp);
		}
		else
			AfxMessageBox(L"没有客户数据，请重新输入！", MB_OK);
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}
}


void CModifyClientDlg::OnSaveClient()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(1);

	CString roomNo, paperNum, deposit, search_num;
	m_nRoomNum.GetWindowText(roomNo);
	m_nPaperNum.GetWindowText(paperNum);
	m_nPrepayment.GetWindowText(deposit);
	m_sPaperNum.GetWindowText(search_num);

	try
	{

		CString strSQL;
		strSQL.Format(L"Update Client Set RoomNo = '%s', \
								PaperNum = '%s', \
								Deposit = '%s' Where PaperNum = '%s'",
			roomNo, paperNum, deposit, search_num);
		HRESULT hr = theApp.m_pConnection->Execute(
			strSQL.AllocSysString(),
			NULL,
			adCmdText);
		if (SUCCEEDED(hr))
			AfxMessageBox(L"更新客户信息成功！", MB_OK);
		CDialog::OnOK();
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}
}
